<template>
    <div class="statistics_box">
        <div class="title">
            <span>趋势图</span>
            <!-- <el-button :disabled="!state.dateList?.length" @click="onExportImg(myChartLineRef)">导出图片</el-button> -->
        </div>
        <div v-if="state.dateList?.length" id="echarts" ref="myChartLineRef"></div>
        <div v-else class="none_data">
            <img class="no_img" src="@/assets/noData.png" alt="" />
            <div>暂无数据</div>
        </div>
    </div>
</template>
<script setup lang="tsx">
import { ref, inject, defineExpose, nextTick } from 'vue';
import { export2Img } from '@/utils/utils';

const echarts: any = inject('ec');
const onExportImg = (chartEl: any) => {
    export2Img(chartEl, '趋势图');
};
const myChartLineRef = ref(null);
const state = ref({
    nameList: [],
    dateList: [],
    dataList: []
});
const getEchartsLine = () => {
    const myChartLine = echarts?.init(myChartLineRef.value);
    const option = {
        color: [
            '#4B7FE8',
            '#A6ED71',
            '#FF9D23',
            '#32D1AD',
            '#FF4412',
            '#FF9D23',
            '#32D1AD',
            '#FF4412',
            '#FF9D23',
            '#32D1AD',
            '#FF4412'
        ],
        tooltip: {
            trigger: 'axis'
            // axisPointer: {
            //   type: 'cross',
            //   label: {
            //     name: 'jasj',
            //     backgroundColor: '#FF6B00'
            //   }
            // }
        },
        legend: {
            x: 'center',
            y: 'bottom',
            bottom: 30,
            padding: [0, 0, 10, 0],
            data: state.value.nameList
        },
        grid: {
            top: 20,
            bottom: 60,
            left: 32,
            right: 40,
            containLabel: true
        },
        xAxis: {
            type: 'category',
            data: state.value.dateList,
            boundaryGap: false,
            // axisTick: {
            //   show: false,
            //   alignWithLabel: true
            // },
            offset: 3
        },
        yAxis: {
            type: 'value',
            name: '人',
            nameGap: 10, // y轴name与横纵坐标轴线的间距
            nameTextStyle: {
                padding: [0, 35, 10, 0] // 加上padding可以调整其位置
            },
            axisTick: {
                show: false
            },
            axisLabel: {
                color: '#969696'
            },
            splitLine: {
                lineStyle: {
                    type: 'line',
                    color: '#e8e8e8' // 分割线颜色
                }
            }
        },
        series: state.value.dataList
    };
    myChartLine.setOption(option);
    window.addEventListener('resize', function () {
        //执行
        if (myChartLine) {
            // 判断一下，防止在初始化的时候报错，cannot read property 'resize' of null
            myChartLine.resize();
        }
    });
};
const onStatistics = (res: any) => {
    state.value.nameList = res.nameList;
    state.value.dateList = res.dateList;
    state.value.dataList = res.nameList.map((item: any, index: any) => {
        return {
            name: item,
            type: 'line',
            stack: 'Total',
            smooth: true,
            symbol: 'circle',
            emphasis: {
                focus: 'series'
            },
            data: res.dataList[index],
            symbolSize: 0
        };
    });
    nextTick(() => {
        getEchartsLine();
    });
};
defineExpose({
    onStatistics
});
</script>
<style scoped lang="scss">
.statistics_box {
    padding: 20px 26px;
    margin-bottom: 16px;
    background: #fff;
    box-shadow: 0 4px 4px 0 rgb(240 240 240 / 50%);
    border: 1px solid #eaeaea;
    font-size: 14px;
    color: #33302d;

    .title {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding-bottom: 14px;
        border-bottom: 1px solid #eaeaea;
        font-size: 16px;
    }

    .btn_box {
        display: flex;
        justify-content: space-between;
        padding: 16px 0;
    }

    #echarts {
        display: flex;
        justify-content: center;
        height: 500px;
    }
}

.circle_box {
    display: flex;
    margin-bottom: 16px;

    .circle {
        flex: 1;
        padding: 20px 26px;
        margin-right: 20px;
        background: #fff;
        box-shadow: 0 4px 4px 0 rgb(240 240 240 / 50%);
        border: 1px solid #eaeaea;

        .circle_title {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding-bottom: 8px;
            border-bottom: 1px solid #eaeaea;
        }
    }

    .margin {
        margin-right: 0;
    }
}

.none_data {
    margin: 100px 0;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    color: #909399;

    img {
        transform: translateX(15%);
    }
}
</style>
